#include "tunemanualframe.h"
#include "ui_tunemanualframe.h"
#include"delegate.h"
#include "algorithm.h"
#include"elementtableframe.h"
#include <QColorDialog>

TuneManualFrame::TuneManualFrame(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::TuneManualFrame)
{
    ui->setupUi(this);

    pTuneManual=new TuneManual;
    FM->LoadFromFile(FM->GetTuneManualParaFile(),pTuneManual);

    pTuneResult=new TuneResult;
    //FM->LoadFromFile(FM->GetTuneFile(),pTuneResult);

    pMySlide1=new MySlide(this,"雾化气","","L/min","");
    pMySlide1->SetScope(0,2,3);
    pMySlide1->SetValueSet(0);
    pMySlide1->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide1);
    pMySlide2=new MySlide(this,"辅助气","","L/min","");
    pMySlide2->SetScope(0,2,2);
    pMySlide2->SetValueSet(0);
    pMySlide2->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide2);
    pMySlide3=new MySlide(this,"冷却气","","L/min","");
    pMySlide3->SetScope(0,20,2);
    pMySlide3->SetValueSet(0);
    pMySlide3->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide3);
    pMySlide4=new MySlide(this,"附加气","","mL/min","");
    pMySlide4->SetScope(0,100,2);
    pMySlide4->SetValueSet(0);
    pMySlide4->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide4);
    pMySlide5=new MySlide(this,"碰撞气H2","","mL/min","");
    pMySlide5->SetScope(0,10,2);
    pMySlide5->SetValueSet(0);
    pMySlide5->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide5);
    pMySlide6=new MySlide(this,"碰撞气He","","mL/min","");
    pMySlide6->SetScope(0,10,2);
    pMySlide6->SetValueSet(0);
    pMySlide6->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide6);
    pMySlide7=new MySlide(this,"六级杆","","V","");
    pMySlide7->SetScope(-20,20,2);
    pMySlide7->SetValueSet(0);
    pMySlide7->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide7);
    pMySlide8=new MySlide(this,"四级杆","","V","");
    pMySlide8->SetScope(-20,20,2);
    pMySlide8->SetValueSet(0);
    pMySlide8->SetValueCurrent(0);
    ui->verticalLayout1->addWidget(pMySlide8);

    pMySlide11=new MySlide(this,"提取透镜","","V","");
    pMySlide11->SetScope(-1000,20,0);
    pMySlide11->SetValueSet(0);
    pMySlide11->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide11);
    pMySlide12=new MySlide(this,"透镜1","","V","");
    pMySlide12->SetScope(-1200,0,0);
    pMySlide12->SetValueSet(0);
    pMySlide12->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide12);
    pMySlide13=new MySlide(this,"透镜2","","V","");
    pMySlide13->SetScope(-200,0,0);
    pMySlide13->SetValueSet(0);
    pMySlide13->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide13);
    pMySlide14=new MySlide(this,"透镜3","","V","");
    pMySlide14->SetScope(-500,0,0);
    pMySlide14->SetValueSet(0);
    pMySlide14->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide14);
    pMySlide15=new MySlide(this,"聚焦透镜","","V","");
    pMySlide15->SetScope(-20,30,1);
    pMySlide15->SetValueSet(0);
    pMySlide15->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide15);
    pMySlide16=new MySlide(this,"偏转透镜1","","V","");
    pMySlide16->SetScope(-200,0,0);
    pMySlide16->SetValueSet(0);
    pMySlide16->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide16);
    pMySlide17=new MySlide(this,"偏转透镜2","","V","");
    pMySlide17->SetScope(-200,0,0);
    pMySlide17->SetValueSet(0);
    pMySlide17->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide17);
    pMySlide18=new MySlide(this,"偏转透镜3","","V","");
    pMySlide18->SetScope(-200,0,0);
    pMySlide18->SetValueSet(0);
    pMySlide18->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide18);
    pMySlide19=new MySlide(this,"偏转透镜4","","V","");
    pMySlide19->SetScope(-200,0,0);
    pMySlide19->SetValueSet(0);
    pMySlide19->SetValueCurrent(0);
    ui->verticalLayout2->addWidget(pMySlide19);

    pMySlide21=new MySlide(this,"横向","向左","mm","向右");
    pMySlide21->SetScope(-4,4,2);
    pMySlide21->SetValueSet(0);
    pMySlide21->SetValueCurrent(0);
    ui->verticalLayout3->addWidget(pMySlide21);
    pMySlide22=new MySlide(this,"纵向","向上","mm","向下");
    pMySlide22->SetScope(-4,4,2);
    pMySlide22->SetValueSet(0);
    pMySlide22->SetValueCurrent(0);
    ui->verticalLayout3->addWidget(pMySlide22);
    pMySlide23=new MySlide(this,"深度","向后","mm","向前");
    pMySlide23->SetScope(0,20,2);
    pMySlide23->SetValueSet(0);
    pMySlide23->SetValueCurrent(0);
    ui->verticalLayout3->addWidget(pMySlide23);

    pMySlide31=new MySlide(this,"模拟","","V","");
    pMySlide31->SetScope(-2500,0,0);
    pMySlide31->SetValueSet(0);
    pMySlide31->SetValueCurrent(0);
    ui->verticalLayout4->addWidget(pMySlide31);
    pMySlide32=new MySlide(this,"计数","","V","");
    pMySlide32->SetScope(0,2500,0);
    pMySlide32->SetValueSet(0);
    pMySlide32->SetValueCurrent(0);
    ui->verticalLayout4->addWidget(pMySlide32);
    pMySlide33=new MySlide(this,"标准分辨率","","V","");
    pMySlide33->SetScope(0,5,2);
    pMySlide33->SetValueSet(0);
    pMySlide33->SetValueCurrent(0);
    ui->verticalLayout5->addWidget(pMySlide33);
    pMySlide34=new MySlide(this,"高分辨率","","V","");
    pMySlide34->SetScope(0,5,2);
    pMySlide34->SetValueSet(0);
    pMySlide34->SetValueCurrent(0);
    ui->verticalLayout5->addWidget(pMySlide34);
    pMySlide35=new MySlide(this,"功率","","W","");
    pMySlide35->SetScope(0,1600,0);
    pMySlide35->SetValueSet(0);
    pMySlide35->SetValueCurrent(0);
    ui->verticalLayout6->addWidget(pMySlide35);
    pMySlide36=new MySlide(this,"雾化室温度","","℃","");
    pMySlide36->SetScope(-15,20,0);
    pMySlide36->SetValueSet(0);
    pMySlide36->SetValueCurrent(0);
    ui->verticalLayout6->addWidget(pMySlide36);
    pMySlide37=new MySlide(this,"蠕动泵","","r/min","");//
    pMySlide37->SetScope(0,150,0);
    pMySlide37->SetValueSet(0);
    pMySlide37->SetValueCurrent(0);
    ui->verticalLayout6->addWidget(pMySlide37);

    CheckBox *pCheckBox;
    DoubleSpinBoxDelegate *pDoubleSpinBox;
    SpinBoxDelegate *pSpinBox;
    LineEditDelegate *pLineEdit;
    pCheckBox=new CheckBox(this,0);
    ui->tableWidget_Hop->setItemDelegateForColumn(0,pCheckBox);
    pCheckBox=new CheckBox(this,0);
    ui->tableWidget_Hop->setItemDelegateForColumn(1,pCheckBox);//
    pLineEdit=new LineEditDelegate(this,3);
    ui->tableWidget_Hop->setItemDelegateForColumn(5,pLineEdit);
    pLineEdit=new LineEditDelegate(this,3);
    ui->tableWidget_Hop->setItemDelegateForColumn(6,pLineEdit);
    pLineEdit=new LineEditDelegate(this,0);
    ui->tableWidget_Hop->setItemDelegateForColumn(7,pLineEdit);
    ComboxDelegate *pComboxDelegate=new ComboxDelegate(this,0);
    ui->tableWidget_Hop->setItemDelegateForColumn(8,pComboxDelegate); 
    ui->tableWidget_Hop->setColumnHidden(0,true);
    ui->tableWidget_Hop->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    pCheckBox=new CheckBox(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(0,pCheckBox);
    pCheckBox=new CheckBox(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(1,pCheckBox);//
    pLineEdit=new LineEditDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(4,pLineEdit);
    pLineEdit=new LineEditDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(5,pLineEdit);
    pLineEdit=new LineEditDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(6,pLineEdit);
    pLineEdit=new LineEditDelegate(this,3);
    ui->tableWidget_Sweep->setItemDelegateForColumn(7,pLineEdit);
    pComboxDelegate=new ComboxDelegate(this,0);
    ui->tableWidget_Sweep->setItemDelegateForColumn(8,pComboxDelegate);
    ui->tableWidget_Sweep->setColumnHidden(0,true);
    //ui->tableWidget_Sweep->setColumnHidden(3,true);
    ui->tableWidget_Sweep->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableWidget_Hop->setContextMenuPolicy(Qt::CustomContextMenu);//ActionsContextMenu:不能分组
    ui->tableWidget_Sweep->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->tableWidget_Hop,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(OnCustomContextMenuRequested(QPoint)));
    connect(ui->tableWidget_Sweep,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(OnCustomContextMenuRequested(QPoint)));
    pMenu = new QMenu(this);
    pMenu->addAction(ui->actionAdd);
    pMenu->addAction(ui->actionDel);

    pQwtPlotSpectral1=new MyQwtPlot;;
    ui->horizontalLayout_Spectral1->insertWidget(0,pQwtPlotSpectral1);
    pQwtPlotSpectral1->setTitle("");
    pQwtPlotSpectral1->setAxisTitle(QwtPlot::xBottom,"质量数（amu）");
    pQwtPlotSpectral1->setAxisTitle(QwtPlot::yLeft,"响应值（cps）");
    pQwtPlotSpectral2=new MyQwtPlot;;
    ui->horizontalLayout_Spectral2->insertWidget(0,pQwtPlotSpectral2);
    pQwtPlotSpectral2->setTitle("");
    pQwtPlotSpectral2->setAxisTitle(QwtPlot::xBottom,"质量数（amu）");
    pQwtPlotSpectral2->setAxisTitle(QwtPlot::yLeft,"响应值（cps）");
    pQwtPlotSpectral3=new MyQwtPlot;;
    ui->horizontalLayout_Spectral3->insertWidget(0,pQwtPlotSpectral3);
    pQwtPlotSpectral3->setTitle("分辨率");
    pQwtPlotSpectral3->setAxisTitle(QwtPlot::xBottom,"质量数");
    pQwtPlotSpectral3->setAxisTitle(QwtPlot::yLeft,"响应值（cps）");

    plotType=0;
    connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(OnPlotTypeChanged(int)));

    pNetDealwith=SM->GetNetDealwith();
    connect(pNetDealwith,SIGNAL(SIG_StateChanged(QString)),this,SLOT(OnStateChanged(QString)));
    connect(pNetDealwith,SIGNAL(SIG_WorkFlowFinished(int)),this,SLOT(OnFlowStepChanged()));
    connect(pNetDealwith,SIGNAL(SIG_ReadValue(WorkStep)),this,SLOT(UpdateTuneValue(WorkStep)));
    connect(pNetDealwith,SIGNAL(SIG_ReadResult(QList<ExpRawData>)),this,SLOT(UpdateTuneResult(QList<ExpRawData>)));

    connect(ui->tabWidget_Scan,SIGNAL(currentChanged(int)),this,SLOT(OnTabScanCurrentChanged(int)));
    connect(ui->tableWidget_Hop,SIGNAL(itemPressed(QTableWidgetItem*)),
            this,SLOT(OnTableWidgetHopItemPress(QTableWidgetItem*)));
    connect(ui->tableWidget_Sweep,SIGNAL(itemPressed(QTableWidgetItem*)),
            this,SLOT(OnTableWidgetSweepItemPress(QTableWidgetItem*)));

    LoadData();
}

TuneManualFrame::~TuneManualFrame()
{
    FM->SaveToFile(FM->GetTuneManualParaFile(),pTuneManual);//

    if(pTuneResult!=NULL)
    {
        delete pTuneResult;
    }
    if(pTuneManual!=NULL)
    {
        delete pTuneManual;
    }
    delete ui;
}

void TuneManualFrame::LoadTunePara()
{
        pMySlide1->SetValue(pTuneResult->atomization);
        pMySlide2->SetValue(pTuneResult->auxiliary);
        pMySlide3->SetValue(pTuneResult->cooler);
        pMySlide4->SetValue(pTuneResult->added);
        pMySlide5->SetValue(pTuneResult->collisionGasH2);
        pMySlide6->SetValue(pTuneResult->collisionGasHe);
        pMySlide7->SetValue(pTuneResult->hexapole);
        pMySlide8->SetValue(pTuneResult->quadrupole);

        pMySlide11->SetValue(pTuneResult->extractionLen);
        pMySlide12->SetValue(pTuneResult->len1);
        pMySlide13->SetValue(pTuneResult->len2);
        pMySlide14->SetValue(pTuneResult->len3);
        pMySlide15->SetValue(pTuneResult->focusingLen);
        pMySlide16->SetValue(pTuneResult->deflectionLen1);
        pMySlide17->SetValue(pTuneResult->deflectionLen2);
        pMySlide18->SetValue(pTuneResult->deflectionLen3);
        pMySlide19->SetValue(pTuneResult->deflectionLen4);

        pMySlide21->SetValue(pTuneResult->motorX);
        pMySlide22->SetValue(pTuneResult->motorY);
        pMySlide23->SetValue(pTuneResult->motorZ);

        pMySlide31->SetValue(pTuneResult->analogDetector);
        pMySlide32->SetValue(pTuneResult->digitalDetector);
        pMySlide33->SetValue(0);//标准分辨率
        pMySlide34->SetValue(0);//高分辨率
        pMySlide35->SetValue(pTuneResult->powerRf);
        pMySlide36->SetValue(0);//雾化室温度
        pMySlide37->SetValue(0);//蠕动泵
}
void TuneManualFrame::SaveTunePara()
{
    pTuneResult->atomization=pMySlide1->GetValueSet();
    pTuneResult->auxiliary=pMySlide2->GetValueSet();
    pTuneResult->cooler=pMySlide3->GetValueSet();
    pTuneResult->added=pMySlide4->GetValueSet();
    pTuneResult->collisionGasH2=pMySlide5->GetValueSet();
    pTuneResult->collisionGasHe=pMySlide6->GetValueSet();
    pTuneResult->hexapole=pMySlide7->GetValueSet();
    pTuneResult->quadrupole=pMySlide8->GetValueSet();

    pTuneResult->extractionLen=pMySlide11->GetValueSet();
    pTuneResult->len1=pMySlide12->GetValueSet();
    pTuneResult->len2=pMySlide13->GetValueSet();
    pTuneResult->len3=pMySlide14->GetValueSet();
    pTuneResult->focusingLen=pMySlide15->GetValueSet();
    pTuneResult->deflectionLen1=pMySlide16->GetValueSet();
    pTuneResult->deflectionLen2=pMySlide17->GetValueSet();
    pTuneResult->deflectionLen3=pMySlide18->GetValueSet();
    pTuneResult->deflectionLen4=pMySlide19->GetValueSet();

    pTuneResult->motorX=pMySlide21->GetValueSet();
    pTuneResult->motorY=pMySlide22->GetValueSet();
    pTuneResult->motorZ=pMySlide23->GetValueSet();

    pTuneResult->analogDetector=pMySlide31->GetValueSet();
    pTuneResult->digitalDetector=pMySlide32->GetValueSet();
    //pMySlide33->SetValue(0);//标准分辨率
    //pMySlide34->SetValue(0);//高分辨率
    pTuneResult->powerRf=pMySlide35->GetValueSet();
    //pMySlide36->SetValue(0);//雾化室温度
    //pMySlide37->SetValue(0);//蠕动泵
}
void TuneManualFrame::OnTuneParaSet()
{
    qDebug()<<"OnTuneParaSet()";
}

void TuneManualFrame::LoadData()
{
    disconnect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    disconnect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));

    int row=0;

    ui->tableWidget_Hop->setRowCount(pTuneManual->GetHopScanParas().count());
    row=0;
    foreach(HopScanPara hopScanPara,pTuneManual->GetHopScanParas())
    {
        int column=0;
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.bIsUsed));
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.bIsDisplay));
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setData(Qt::DecorationRole,hopScanPara.color);//
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        //pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(hopScanPara.name);//
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        //pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setData(Qt::DisplayRole,hopScanPara.massNum);
        //pItem->setText(QString::number(hopScanPara.massNum));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.dwelltime));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.chanels));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(hopScanPara.interval));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(hopScanPara.type);
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        //pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(QString::number(hopScanPara.data));
        ui->tableWidget_Hop->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        //pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(QString::number(hopScanPara.dataRsd));
        ui->tableWidget_Hop->setItem(row,column++,pItem);

        row++;
    }
    ui->tableWidget_Hop->sortByColumn(4,Qt::AscendingOrder);

    int count=pTuneManual->GetSweepScanParas().count();
    ui->tableWidget_Sweep->setRowCount(count);
    for(row=0;row<count;row++)
    {
        SweepScanPara sweepScanPara=pTuneManual->GetSweepScanParas().at(row);
        int column=0;
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.bIsUsed));
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.bIsDisplay));
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setData(Qt::DecorationRole,sweepScanPara.color);//
        ui->tableWidget_Sweep->setItem(row,column++,pItem);

        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(row));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);//
        pItem=new QTableWidgetItem;
        pItem->setData(Qt::DisplayRole,sweepScanPara.startMassNum);
        //pItem->setText(QString::number(sweepScanPara.startMassNum));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.endMassNum));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.dwelltime));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sweepScanPara.channelsAmu));
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sweepScanPara.type);
        ui->tableWidget_Sweep->setItem(row,column++,pItem);
    }
    ui->tableWidget_Sweep->sortByColumn(4,Qt::AscendingOrder);//

    connect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    connect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));

    SystemState systemState=SM->GetSystemState();
    EnableScanPara((systemState.START_STOP==1)?false:true);
}
void TuneManualFrame::SaveData()
{

}
void TuneManualFrame::EnableTunePara(bool state)
{
    ui->scrollArea->setEnabled(state);
    ui->scrollArea_2->setEnabled(state);
    ui->scrollArea_3->setEnabled(state);
    ui->scrollArea_4->setEnabled(state);
}
void TuneManualFrame::EnableScanPara(bool state)
{
    disconnect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    disconnect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));

    int row,column;
    row=ui->tableWidget_Hop->rowCount();
    column=ui->tableWidget_Hop->columnCount();
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<column;j++)
        {
            if(j==3||j==4||j==9||j==10)
            {
                QTableWidgetItem *pItem=ui->tableWidget_Hop->item(i,j);
                pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
            }
            else
            {
                if(state==true)
                {
                    QTableWidgetItem *pItem=ui->tableWidget_Hop->item(i,j);
                    pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);
                }
                else
                {
                    QTableWidgetItem *pItem=ui->tableWidget_Hop->item(i,j);
                    pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
                }
            }
        }
    }

    row=ui->tableWidget_Sweep->rowCount();
    column=ui->tableWidget_Sweep->columnCount();
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<column;j++)
        {
            if(state==true)
            {
                ui->tableWidget_Sweep->item(i,j)->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);
            }
            else
            {
                ui->tableWidget_Sweep->item(i,j)->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
            }
        }
    }

    connect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    connect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));
}
void TuneManualFrame::OnFlowStepChanged()
{
    EnableTunePara(true);
}
//
void TuneManualFrame::OnStateChanged(QString str)
{
    SystemState systemState=SM->GetSystemState();
    EnableTunePara((systemState.START_STOP==1)?false:true);
}

//action
void TuneManualFrame::on_pushButton_Load_clicked()
{
    QString filePath=QFileDialog::getOpenFileName(this, tr("Open File"),FM->GetTuneManualDir(),
                                                  tr("Bin files (*%1)").arg(FM->GetTuneManualSuffix()));
    if(filePath=="") return;

    FM->LoadFromFile(filePath,pTuneResult);
    LoadTunePara();
}
void TuneManualFrame::on_pushButton_Save_clicked()
{
    SaveTunePara();
    QMessageBox::StandardButton button=QMessageBox::question(this,tr("询问"),tr("是否将当前调谐参数更新到点火流程中？"));
    if(button==QMessageBox::Yes)
    {
        FM->SaveToFile(FM->GetTuneFile(),pTuneResult);
        WorkFlow workFlow=SM->GetWorkFlow();
        QString name="打开等离子体";
        if(workFlow.IsExistFlow(name))
        {
            QList<WorkStep> steps=workFlow.GetWorkSteps(name);
            if(steps.isEmpty()==false)
            {
                WorkStep step=steps.last();
                if(step.instrumentState=="分析就绪")
                {
                    step.atomization=pTuneResult->atomization;
                    step.auxiliary=pTuneResult->auxiliary;
                    step.cooler=pTuneResult->cooler;
                    step.added=pTuneResult->added;
                    step.collisionGasH2=pTuneResult->collisionGasH2;
                    step.collisionGasHe=pTuneResult->collisionGasHe;
                    step.hexapole=pTuneResult->hexapole;
                    step.quadrupole=pTuneResult->quadrupole;
                    step.extractionLen=pTuneResult->extractionLen;
                    step.len1=pTuneResult->len1;
                    step.len2=pTuneResult->len2;
                    step.len3=pTuneResult->len3;
                    step.focusingLen=pTuneResult->focusingLen;
                    step.deflectionLen1=pTuneResult->deflectionLen1;
                    step.deflectionLen2=pTuneResult->deflectionLen2;
                    step.deflectionLen3=pTuneResult->deflectionLen3;

                    step.deflectionLen4=pTuneResult->deflectionLen4;
                    step.motorX=pTuneResult->motorX;
                    step.motorY=pTuneResult->motorY;
                    step.motorZ=pTuneResult->motorZ;
                    step.analogDetector=pTuneResult->analogDetector;
                    step.digitalDetector=pTuneResult->digitalDetector;
                    //step.digitalDetector=pTuneResult->digitalDetector;//标准分辨率
                   // step.digitalDetector=pTuneResult->digitalDetector;//高分辨率
                    step.powerRf=pTuneResult->powerRf;
                    //step.digitalDetector=pTuneResult->digitalDetector;//雾化室温度
                    //step.digitalDetector=pTuneResult->digitalDetector;//蠕动泵
                    workFlow.Replace(name,steps.count()-1,step);

                    SM->SetWorkFlow(workFlow);
                    int state=FM->SaveToFile(FM->GetWorkFlowFile(),&workFlow);
                }
            }
        }
    }
    else if(button==QMessageBox::No)
    {

    }
    else if(button==QMessageBox::Cancel||QMessageBox::Close)
    {

    }


    QString filePath=QFileDialog::getSaveFileName(this, tr("Save File"),FM->GetTuneManualDir(),
                                                  tr("Bin files (*%1)").arg(FM->GetTuneManualSuffix()));
    if(filePath=="") return;

    FM->SaveToFile(filePath,pTuneResult);
}

//edit
void TuneManualFrame::OnTableWidgetHopItemPress(QTableWidgetItem *item)
{
    SystemState systemState=SM->GetSystemState();
    if(systemState.START_STOP==1) return;

    int row=item->row();
    int column=item->column();
    if(column!=2) return;

    QColor color=QColorDialog::getColor();
    if(color.isValid()==false) return;

    disconnect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    item->setData(Qt::DecorationRole,color);
    connect(ui->tableWidget_Hop,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Hop_itemChanged(QTableWidgetItem*)));
    on_tableWidget_Hop_itemChanged(item);
}
void TuneManualFrame::OnTableWidgetSweepItemPress(QTableWidgetItem *item)
{
    SystemState systemState=SM->GetSystemState();
    if(systemState.START_STOP==1) return;

    int row=item->row();
    int column=item->column();
    if(column!=2) return;

    QColor color=QColorDialog::getColor();
    if(color.isValid()==false) return;

    disconnect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));
    item->setData(Qt::DecorationRole,color);
    connect(ui->tableWidget_Sweep,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Sweep_itemChanged(QTableWidgetItem*)));
    on_tableWidget_Sweep_itemChanged(item);
}

void TuneManualFrame::on_tableWidget_Hop_itemChanged(QTableWidgetItem *item)
{
    HopScanPara hopScanPara;
    QString name;
    int column,row;
    if(item->column()>=9) return;//cps rsd

    column=0;
    row=item->row();
    name=ui->tableWidget_Hop->item(row,3)->text();
    hopScanPara=pTuneManual->GetHopScanPara(name);

    hopScanPara.bIsUsed=ui->tableWidget_Hop->item(row,column++)->text().toInt();
    hopScanPara.bIsDisplay=ui->tableWidget_Hop->item(row,column++)->text().toInt();
    hopScanPara.color=ui->tableWidget_Hop->item(row,column++)->data(Qt::DecorationRole).value<QColor>();
    hopScanPara.name=ui->tableWidget_Hop->item(row,column++)->text();
    hopScanPara.massNum=ui->tableWidget_Hop->item(row,column++)->text().toDouble();
    hopScanPara.dwelltime=ui->tableWidget_Hop->item(row,column++)->text().toDouble();
    hopScanPara.chanels=ui->tableWidget_Hop->item(row,column++)->text().toInt();
    hopScanPara.interval=ui->tableWidget_Hop->item(row,column++)->text().toDouble();
    hopScanPara.type=ui->tableWidget_Hop->item(row,column++)->text();
    pTuneManual->SetHopScanPara(hopScanPara.name,hopScanPara);

    //LoadData();
}

void TuneManualFrame::on_tableWidget_Sweep_itemChanged(QTableWidgetItem *item)
{
    SweepScanPara sweepScanPara;
    int column,row;
    int index;

    column=0;
    row=item->row();

    sweepScanPara.bIsUsed=ui->tableWidget_Sweep->item(row,column++)->text().toInt();
    sweepScanPara.bIsDisplay=ui->tableWidget_Sweep->item(row,column++)->text().toInt();
    sweepScanPara.color=ui->tableWidget_Sweep->item(row,column++)->data(Qt::DecorationRole).value<QColor>();
    index=ui->tableWidget_Sweep->item(row,column++)->text().toInt();//
    sweepScanPara.startMassNum=ui->tableWidget_Sweep->item(row,column++)->text().toDouble();
    sweepScanPara.endMassNum=ui->tableWidget_Sweep->item(row,column++)->text().toDouble();
    sweepScanPara.dwelltime=ui->tableWidget_Sweep->item(row,column++)->text().toDouble();
    sweepScanPara.channelsAmu=ui->tableWidget_Sweep->item(row,column++)->text().toInt();
    sweepScanPara.type=ui->tableWidget_Sweep->item(row,column++)->text();
    pTuneManual->SetSweepScanPara(index,sweepScanPara);
    qDebug()<<"row"<<row<<"index"<<index;
    //check 合并成功刷新 LoadData
    //LoadData();// 选中第一行
}

//menu
void TuneManualFrame::OnCustomContextMenuRequested(const QPoint &pos)
{
    SystemState systemState=SM->GetSystemState();
    if(systemState.START_STOP==1) return;

    menuType=ui->tabWidget_Scan->currentIndex();
    pMenu->exec(QCursor::pos());//在当前鼠标位置显示
}

void TuneManualFrame::on_actionDel_triggered()
{
    switch(menuType)
    {
        case 0:
            {
                int row=ui->tableWidget_Hop->currentRow();
                QString name=ui->tableWidget_Hop->item(row,3)->text();
                pTuneManual->RemoveHopScanPara(name);
            }
        break;
        case 1:
            {
                int row=ui->tableWidget_Sweep->currentRow();
                int index=ui->tableWidget_Sweep->item(row,3)->text().toInt();
                pTuneManual->RemoveSweepScanPara(index);
            }
        break;
    }
    LoadData();
}

void TuneManualFrame::on_actionAdd_triggered()
{
    switch(menuType)
    {
        case 0:
            {
                ElementTableFrame *pElementTableFrame=new ElementTableFrame(this,SM->GetAnalyteDataBase(),2);
                connect(pElementTableFrame,SIGNAL(SIG_ChooseEle(double,QString)),this,SLOT(OnAddEle(double,QString)));
                pElementTableFrame->setWindowFlags(Qt::Dialog);//|Qt::FramelessWindowHint
                pElementTableFrame->setAttribute(Qt::WA_ShowModal, true);
                pElementTableFrame->show();
            }
        break;
        case 1:
            {
                OnAddEle(0,"QString");
            }
        break;
    }

}
void TuneManualFrame::OnAddEle(double massNum,QString name)
{
    int nucleus=qRound(massNum);
    switch(menuType)
    {
        case 0:
            {
                HopScanPara hopScanPara;
                hopScanPara.name=QString::number(nucleus)+name;
                hopScanPara.massNum=massNum;
                pTuneManual->AddHopScanPara(hopScanPara.name,hopScanPara);
            }
        break;
        case 1:
            {
                SweepScanPara sweepScanPara;
                pTuneManual->AddSweepScanPara(sweepScanPara);
            }
        break;
    }

    LoadData();
}

//start stop
void TuneManualFrame::OnTabScanCurrentChanged(int index)
{
    scanType=index;

    on_pushButton_Stop_clicked();
}

void TuneManualFrame::on_pushButton_Scan_clicked()
{
     SystemState systemState=SM->GetSystemState();
     if(systemState.START_STOP==1)
     {
         QMessageBox::information(this,tr("提示"),tr("仪器正在扫描，稍后再试！"));
         return;
     }
     ui->pushButton_Scan->setEnabled(false);
     pNetDealwith->StartTune(true);

     //EnableTunePara(false);
     EnableScanPara(false);

     //初始化趋势数据
     QList<ScanPara> scanParas;
     if(scanType==0)
     {
         scanParas=Algorithm::GetHopScanPara(pTuneManual->GetHopScanParas());
     }
     else if(scanType==1)
     {
         scanParas=Algorithm::GetSweepScanPara(pTuneManual->GetSweepScanParas());
     }
     for(int i=0;i<scanParas.count();i++)
     {
         QList<double> datas;
         trendDatass.append(datas);
         DatassTemp.append(datas);
     }   
}

void TuneManualFrame::on_pushButton_Stop_clicked()
{
    SystemState systemState=SM->GetSystemState();
    if(systemState.START_STOP==0) return;

     pNetDealwith->StartTune(false);
     ui->pushButton_Scan->setEnabled(true);
     //EnableTunePara(true);
     EnableScanPara(true);
     //clear
     trendDatass.clear();
     DatassTemp.clear();
}

//update
void TuneManualFrame::OnPlotTypeChanged(int index)
{
    plotType=index;
    ui->stackedWidget->setCurrentIndex(index);
    if(plotType==0)
    {
        ui->groupBox_4->setEnabled(true);
    }
    else
    {
        ui->groupBox_4->setEnabled(false);
    }

    //
    if(plotType!=1) return;
    for(int i=0;i<trendDatass.count();i++)
    {
        trendDatass[i].clear();
    }
}

void TuneManualFrame::UpdateTuneValue(WorkStep workStep)
{
    pMySlide1->SetValueCurrent(workStep.atomization);
    pMySlide2->SetValueCurrent(workStep.auxiliary);
    pMySlide3->SetValueCurrent(workStep.cooler);
    pMySlide4->SetValueCurrent(workStep.added);
    pMySlide5->SetValueCurrent(workStep.collisionGasH2);
    pMySlide6->SetValueCurrent(workStep.collisionGasHe);
    pMySlide7->SetValueCurrent(workStep.hexapole);
    pMySlide8->SetValueCurrent(workStep.quadrupole);

    pMySlide11->SetValueCurrent(workStep.extractionLen);
    pMySlide12->SetValueCurrent(workStep.len1);
    pMySlide13->SetValueCurrent(workStep.len2);
    pMySlide14->SetValueCurrent(workStep.len3);
    pMySlide15->SetValueCurrent(workStep.focusingLen);
    pMySlide16->SetValueCurrent(workStep.deflectionLen1);
    pMySlide17->SetValueCurrent(workStep.deflectionLen2);
    pMySlide18->SetValueCurrent(workStep.deflectionLen3);
    pMySlide19->SetValueCurrent(workStep.deflectionLen4);

    pMySlide21->SetValueCurrent(workStep.motorX);
    pMySlide22->SetValueCurrent(workStep.motorY);
    pMySlide23->SetValueCurrent(workStep.motorZ);

    pMySlide31->SetValueCurrent(workStep.analogDetector);
    pMySlide32->SetValueCurrent(workStep.digitalDetector);
    pMySlide33->SetValueCurrent(workStep.highResolution);//标准分辨率
    pMySlide34->SetValueCurrent(workStep.standardResolution);//高分辨率
    pMySlide35->SetValueCurrent(workStep.powerRf);
    pMySlide36->SetValueCurrent(workStep.atomizationT);//雾化室温度
    pMySlide37->SetValueCurrent(workStep.peristalticPumpSpeed);//蠕动泵
}

void TuneManualFrame::UpdateTuneResult(QList<ExpRawData> rawDatas)
{
    this->rawDatas=rawDatas;
//    int count=massCurves.count();
//    if(count<=0) return; 
    foreach(QwtPlotCurve *pCurve,curves)
    {
        delete pCurve;
    }
    curves.clear();

    QList<ScanPara> scanParas;
    int index;
    if(scanType==0)
    {
        scanParas=Algorithm::GetHopScanPara(pTuneManual->GetHopScanParas());
        index=0;
        for(int i=0;i<scanParas.count();i++)
        {
            //是否勾选...
            int noChoose=0;//

            int channel=scanParas.at(i).channels;
            double sum=0;
            for(int j=0;j<channel;j++)
            {
                if(index>=rawDatas.count()) break;
                if(ui->radioButton_Analogy->isChecked())
                {
                    sum+=rawDatas.at(index).analogValue;
                }
                else
                {
                    sum+=rawDatas.at(index).analogValue;
                }
                index++;
            }

            DatassTemp[i].append(sum);
            if(DatassTemp[i].count()>10)
            {
               DatassTemp[i].removeFirst();
            }

            //计算 sum rsd
            double cps=0,rsd=0;
            int count=DatassTemp[i].count();
            if(count<10)//3
            {
                rsd=0;
            }
            else
            {
                rsd=Algorithm::SdData(DatassTemp[i]);
            }
            if(count>0)
            {
                cps=DatassTemp[i].at(count-1);
            }
            //HopScanPara hopScanPara=pTuneManual->GetHopScanPara(scanParas.at(i).dwelltime);

            ui->tableWidget_Hop->item(i,9)->setText(QString::number(cps,'g',4));
            ui->tableWidget_Hop->item(i,10)->setText(QString::number(rsd,'f',2));
        }
    }
    else if(scanType==1)
    {
        scanParas=Algorithm::GetSweepScanPara(pTuneManual->GetSweepScanParas());
    }

    index=0;
    switch(plotType)
    {
        case 0:
            for(int i=0;i<scanParas.count();i++)
            {
                int channel=scanParas.at(i).channels;
                double startMass=scanParas.at(i).startMassNum;
                double interval=scanParas.at(i).interval;
                bool state=scanParas.at(i).bIsDisplay;
                QColor color=scanParas.at(i).color;

                QwtPlotCurve *pCurve = new QwtPlotCurve;
                pCurve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
                pCurve->attach(pQwtPlotSpectral1);
                pCurve->setVisible(state);
                curves.append(pCurve);

                if(ui->radioButton_Curve->isChecked())
                {
                    pCurve->setPen(color);
                    pCurve->setStyle(QwtPlotCurve::Lines);
                }
                else
                {
                    pCurve->setPen(color,5);
                    pCurve->setStyle(QwtPlotCurve::Sticks);
                }
                //
                QList<double> xs=Algorithm::GetNumList(channel,startMass,interval);
                QList<double> ys;
                for(int j=0;j<channel;j++)
                {
                    if(index>=rawDatas.count()) break;
                    if(ui->radioButton_Analogy->isChecked())
                    {
                        ys.append(rawDatas.at(index).analogValue);
                    }
                    else
                    {
                        ys.append(rawDatas.at(index).plusedValue);
                    }
                    index++;
                }
                pCurve->setSamples(xs.toVector(),ys.toVector());
            }
            pQwtPlotSpectral1->replot();
            break;
        case 1:
            for(int i=0;i<scanParas.count();i++)
            {
                int channel=scanParas.at(i).channels;
                bool state=scanParas.at(i).bIsDisplay;
                QColor color=scanParas.at(i).color;

                QwtPlotCurve *pCurve = new QwtPlotCurve;
                pCurve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
                pCurve->setPen(color);
                pCurve->attach(pQwtPlotSpectral2);
                pCurve->setVisible(state);
                curves.append(pCurve);

                double sum=0;
                for(int j=0;j<channel;j++)
                {
                    if(index>=rawDatas.count()) break;
                    if(ui->radioButton_Analogy->isChecked())
                    {
                        sum+=rawDatas.at(index).analogValue;
                    }
                    else
                    {
                        sum+=rawDatas.at(index).analogValue;
                    }
                    index++;
                }
                trendDatass[i].append(sum);
                QList<double> xs=Algorithm::GetNumList(trendDatass[i].count(),0,0.01);//

                pCurve->setSamples(xs.toVector(),trendDatass[i].toVector());
            }
            pQwtPlotSpectral2->replot();
            break;
        case 2:
            UpdateResolutionPlot();
            break;
    }
}

void TuneManualFrame::UpdateResolutionPlot()
{
    QList<ScanPara> scanParas;
    int index,row;
    QModelIndex cur;
    double resolutionValue;
    if(scanType==0)
    {
       cur=ui->tableWidget_Hop->currentIndex();
       scanParas=Algorithm::GetHopScanPara(pTuneManual->GetHopScanParas());
    }
    else
    {
       cur=ui->tableWidget_Sweep->currentIndex();
       scanParas=Algorithm::GetSweepScanPara(pTuneManual->GetSweepScanParas());
    }

    if(cur.isValid()==false) return;//

    row=cur.row();
    index=0;
    for(int i=0;i<scanParas.count();i++)
    {
        int channel=scanParas.at(i).channels;
        double startMass=scanParas.at(i).startMassNum;
        double interval=scanParas.at(i).interval;
        bool state=scanParas.at(i).bIsDisplay&(i==row);
        QColor color=scanParas.at(i).color;

        QwtPlotCurve *pCurve = new QwtPlotCurve;
        pCurve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
        pCurve->setPen(color);
        pCurve->attach(pQwtPlotSpectral3);
        pCurve->setVisible(state);
        curves.append(pCurve);

        QwtSymbol *symbol=new QwtSymbol;
        symbol->setColor(color);
        symbol->setStyle(QwtSymbol::Rect);
        symbol->setSize(5,5);
        pCurve->setSymbol(symbol);

        //
        QList<double> xs=Algorithm::GetNumList(channel,startMass,interval);
        QList<double> ys;
        for(int j=0;j<channel;j++)
        {
            if(index>=rawDatas.count()) break;
            if(ui->radioButton_Analogy->isChecked())
            {
                ys.append(rawDatas.at(index).analogValue);
            }
            else
            {
                ys.append(rawDatas.at(index).plusedValue);
            }
            index++;
        }

        pCurve->setSamples(xs.toVector(),ys.toVector());

        if(state==true)//计算分辨率
        {
            resolutionValue=Algorithm::CalResolution(xs,ys);
        }
    }
    pQwtPlotSpectral3->setTitle(tr("分辨率(%1)").arg(resolutionValue));
    pQwtPlotSpectral3->replot();
}

//1.手动改变调谐变量值 何时发送？

